import BaseAnimation from "./BaseAnimation";
import StringHelper from "../tools/StringHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class FrameAnimation extends BaseAnimation {

    @property(cc.SpriteAtlas)
    atlas: cc.SpriteAtlas = null;


    @property(cc.Sprite)
    sprite: cc.Sprite = null;

    @property
    startFrame: number = 0;

    @property
    endFrame: number = 0;

    @property
    duration: number = 0.1;

    private isPlaying: boolean = false

    private frame: number = 0;

    private dt: number = 0;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    init() {
        this.initFlag = true;
        if (!this.sprite) {
            this.sprite = this.addComponent(cc.Sprite);
            // cc.log(' this.actionName + this.frame ',this.actionName + this.frame)
            this.sprite.spriteFrame = this.atlas.getSpriteFrame(this.actionName + this.frame)

        }
        if (this.autoPlay) {
            this.play()
        }
    }

    create(name: string,startFrame: number, endFrame: number, autoPlayer: boolean = false) {
        // if(!this.animation){
        // this.animation = this.addComponent(FrameAnimation)
        // this.animation.setAtlas(atlas)
        this.setActionName(name)
        this.setStartFrame(startFrame)
        this.setEndFrame(endFrame)
        this.setAutoPlay(autoPlayer)
        this.init();
        // }
    }
    setActionName(name: string) {
        super.setActionName(StringHelper.laststring(name, '/'))
    }
    setAtlas(atlas: cc.SpriteAtlas) {
        this.atlas = atlas;
    }

    setDuration(dur: number) {
        this.duration = dur
    }

    setStartFrame(index: number) {
        this.startFrame = index;
    }

    setEndFrame(index: number) {
        this.endFrame = index;
    }


    play(actionName?: string, count: number = -1) {
        this.count = count;
        this.isPlaying = true;
        this.frame = this.startFrame;
        this.dt = 0;
    }

    start() {
        if (this.initFlag) {
            return;
        }

        this.init();
    }

    update(dt) {
        // cc.log(' this.isPlaying  ',this.isPlaying,'  this.dt ', this.dt,this.frame,this.actionName )
        if (this.isPlaying) {
            this.dt += dt;
            if (this.dt >= this.duration) {
                this.dt = 0;
                this.frame++;
                if (this.frame > this.endFrame) {
                    if (this.count == -1) {
                        this.frame = this.startFrame;
                    } else {
                        this.frame = this.endFrame;
                        this.isPlaying = false
                    }
                    this.onFinished();
                } else {
                    let name = this.actionName + this.frame;
                    this.sprite.spriteFrame = this.atlas.getSpriteFrame(name)
                }

            }
        }
    }
    /**
     * 暂停
     */
    pause() {
        this.isPlaying = false
    }

    /**
     * 恢复
     */
    resume() {
        this.isPlaying = true
    }
    // update (dt) {}
}
